//----------------------------------------------------------------------------
// Window 'Kaart'
//----------------------------------------------------------------------------
Ti.include('lib/include.js');
Ti.include('lib/constants.js');
Ti.include('lib/strings.js');



var myLocation ;
var Map = require('ti.map');
var winMap = Titanium.UI.currentWindow;
winMap.addEventListener('focus', function(e) {
   log("Window 'Kaart' actief");
   getCurrentPosition();
});

var locations = [];
locations.push(Map.createAnnotation({
   latitude : 50.842496,
   longitude : 5.727246,
   title : T_MAP_ODK_TITLE,
   subtitle : T_MAP_ODK_SUBTITLE,
   leftButton: '/images/icons/07-map-marker.png',
   pincolor : Map.ANNOTATION_RED,
   animate : true,
   id : ODK_ID
}));

locations.push(Map.createAnnotation({
   latitude : 50.838250,
   longitude : 5.73310,
   title : T_MAP_NBS_TITLE,
   subtitle : T_MAP_NBS_SUBTITLE,
   leftButton: '/images/icons/07-map-marker.png',
   pincolor : Map.ANNOTATION_RED,
   animate : true,
   id : NBS_ID
}));



//----------------------------------------------------------------------------
// View
//----------------------------------------------------------------------------
var mapview = Map.createView({
   mapType : Map.STANDARD_TYPE,
   region : {
      latitude : 50.840000,
      longitude : 5.73000,
      latitudeDelta : 0.009,
      longitudeDelta : 0.009
   },
   animate : true,
   regionFit : true,
   userLocation : true,
   enableZoomControls: true,
   userLocationButton: true,
   userLocation: true,
   annotations: locations
});
mapview.addEventListener('click',function(e){
    if (e.clicksource == 'leftButton') {
       if (isIOS7Plus) {
          setRouteToLocation(e.annotation.id);
       }
    }
});


//----------------------------------------------------------------------------
// Toolbar
//----------------------------------------------------------------------------

//Zoom in button
var btnZoomIn = Titanium.UI.createButton({
   image:'/images/icons/plus32.png'
});
btnZoomIn.addEventListener('click', function(e) {
   mapview.zoom(1);
});

//Zoom out button
var btnZoomOut = Titanium.UI.createButton({
   image:'/images/icons/minus32.png'
});
btnZoomOut.addEventListener('click', function(e) {
   mapview.zoom(-1);
});

//Standaard map type button
var btnMapStd = Titanium.UI.createButton({
   title: 'Std'
});
btnMapStd.addEventListener('click', function(e) {
   mapview.setMapType(Map.STANDARD_TYPE);
});

//Hybrid map type button
var btnMapHybrid = Titanium.UI.createButton({
   title: 'Hyb'
});
btnMapHybrid.addEventListener('click', function(e) {
   mapview.setMapType(Map.HYBRID_TYPE);
});

//Satelliet map type button
var btnMapSat = Titanium.UI.createButton({
   title: 'Sat'
});
btnMapSat.addEventListener('click', function(e) {
   mapview.setMapType(Map.SATELLITE_TYPE);
});

var flexSpace = Titanium.UI.createButton({
   systemButton:Titanium.UI.iPhone.SystemButton.FLEXIBLE_SPACE
});

//toolbar
var mapToolBar = Titanium.UI.iOS.createToolbar({
    items:[btnZoomIn, btnZoomOut, flexSpace, btnMapStd, btnMapHybrid, btnMapSat],
    bottom:0,
    borderTop:false,
    borderBottom:false
}); 
if (isIOS7Plus()) {
   //Do NOT set opacity to anything but 1.0
   mapToolBar.setOpacity(1.0);
}
else {
   mapToolBar.setBarColor(donkergroen);
   mapToolBar.setColor(wit);   
}


//----------------------------------------------------------------------------
// Add UI elements
//----------------------------------------------------------------------------
winMap.add(mapview, mapToolBar);



//----------------------------------------------------------------------------
// Functions
//----------------------------------------------------------------------------
function setRouteToLocation(loc) {
   if (myLocation == undefined) {
      log('Huidige positie nog niet bepaald.');
      myLocation = getCurrentLocation();
   }
   if (loc == ODK_ID) {
      log("Route bepalen van huidige positie (" +myLocation +") naar lokatie Onder de Kerk (" +ODK_LOC +")");
      var str = 'http://maps.apple.com/?daddr='+ ODK_LOC +'&saddr='+myLocation;
      log(str);
      Ti.Platform.openURL(str);
   } 
   if (loc == NBS_ID) {
      log("Route bepalen van huidige positie (" +myLocation +") naar lokatie Niels Bohrstraat (" +NBS_LOC +")");
      var str = 'http://maps.apple.com/?daddr='+ NBS_LOC +'&saddr='+myLocation;
      log(str);
      Ti.Platform.openURL(str);
   }
}


function getCurrentPosition() {
   log('Huidige positie (GPS coordinaten) ophalen');
   if (!Ti.Geolocation.locationServicesEnabled) {
      alert('Activeer aub locatie voorzieningen');
      return null;
   }

   //IOS only
   Titanium.Geolocation.purpose = 'Huidige positie bepalen';
   Titanium.Geolocation.getCurrentPosition(function(e) {
      if (e.error) {
         Ti.API.error('Fout: ' + e.error);
         return null;
      }
      myLocation = e.coords.latitude +',' +e.coords.longitude;     
      log('[*] myLocation: ' +myLocation);
      return myLocation;         
   });
}
